package tests;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import filter.FilterPostsByAuthorName;
import filter.FilterPostsByTitle;
import gestioneDatabase.Post.AllPostsRetriever;
import gestioneDatabase.Post.PostsManager;

import java.util.ArrayList;

import models.Post;
import models.User;

import org.junit.Before;
import org.junit.Test;

public class TestPostsManager {

	PostsManager testPostManager = PostsManager.getManager();
	Post testPost = null;
	User testUser = null;
	AllPostsRetriever testPostsRetriever = null;

	
	@Before
	public void createTestPostList(){
		testUser = new User("user1");
		testPost = new Post(testUser);
		testPost.setTitle("title1");
		testPost.setContent("Contenuto di test");
		testPost.setStyle("capuccino");
		testPost.setId(1);
		testPostsRetriever = new AllPostsRetriever();
	}
	
	@Test
	public void testGetAllPostsList() {
		ArrayList<Post> testResults = testPostManager.getAllPostsList();
		testPostsRetriever = new AllPostsRetriever();
		ArrayList<Post> testPosts = testPostsRetriever.getPosts();
		assertTrue(testResults.size()==testPosts.size());
	}
	
	@Test
	public void testInsertPost(){
		ArrayList<Post> testPosts = testPostsRetriever.getPosts();
		testPostManager.insertPost(testPost.getAuthor(), testPost.getTitle(),
				testPost.getContent(), testPost.getStyle());
		ArrayList<Post> testResults = testPostManager.getAllPostsList();	
		assertTrue(testResults.size()==testPosts.size()+1);
	}
	
	@Test
	public void testSearchPostsWithFilter(){
		FilterPostsByAuthorName filterByName = new FilterPostsByAuthorName("test"); 
		FilterPostsByTitle filterByTitle = new FilterPostsByTitle("test");
		ArrayList<Post> testMergedResults = testPostManager.searchPostsWithFilter("test");
		ArrayList<Post> testResults = filterByName.applyFilter(testPostsRetriever.getPosts());
		testResults.addAll(filterByTitle.applyFilter(testPostsRetriever.getPosts()));
		assertTrue(testMergedResults.size() == testResults.size());
	}
	
	@Test
	public void testGetPostByID() {
		testPost.setTitle("TestTitolo1");
		testPost.setContent("TestContenuto1");
		testPost.setStyle("mint");
		testPostManager.insertPost(testUser, testPost.getTitle(), testPost.getContent(), testPost.getStyle());
		assertEquals(testPostsRetriever.getPosts().get(testPostsRetriever.getPosts().size()-1).getId(), 
				testPostManager.getPostByID(testPostsRetriever.getPosts().size()-testPost.getId()).getId());
	}


	@Test
	public void testFillSinglePost() {
		Post fillPost = testPostManager.fillSinglePost(2, "user2", "title2", "Contenuto di test", "capuccino");
		assertTrue((fillPost.getAuthor().getFullName().equals("user2") && fillPost.getId().equals(2) && fillPost.getTitle().equals("title2"))
				&& (fillPost.getContent().equals("Contenuto di test") && fillPost.getStyle().equals("capuccino")));
	}
}
